package com.liu.voluntary.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.liu.voluntary.mapper.ActivityMapper;
import com.liu.voluntary.mapper.OrderMapper;
import com.liu.voluntary.mapper.UserMapper;
import com.liu.voluntary.pojo.Activity;
import com.liu.voluntary.pojo.Admin;
import com.liu.voluntary.pojo.Order;
import com.liu.voluntary.pojo.User;
import com.liu.voluntary.service.ActivityService;
import com.liu.voluntary.service.OrderService;
import com.liu.voluntary.service.UserService;
import com.liu.voluntary.util.CookieUtil;
import com.liu.voluntary.vo.RespBeanEnum;
import com.sun.org.apache.xpath.internal.operations.Or;
import org.springframework.boot.autoconfigure.cache.CacheProperties;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Controller

public class OrderController {

    @Resource
    private ActivityMapper activityMapper;

    @Resource
    private OrderService orderService;

    @Resource
    private RedisTemplate redisTemplate;


    @RequestMapping("/doSeckill")
    public String doSeckill(Model model, @CookieValue("Ticket") String Ticket, HttpServletRequest request, HttpServletResponse response, @RequestParam(value = "activityId") String activityId) {




        String ticket = CookieUtil.getCookieValue(request, "Ticket");

        User user = (User) redisTemplate.opsForValue().get("user:" + Ticket);
//        User user = (User) request.getSession().getAttribute(ticket);
        if (user == null) {
            return "login";
        }

        model.addAttribute(user);

        Activity activity = activityMapper.selectById(activityId);

        //判断是否有库存
        if (activity.getNumber() < 1) {
            model.addAttribute("errmsg", RespBeanEnum.ENTRY_STOCK.getMessage());
            return "secKillFail";
        }

        //判断是否复购
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", user.getId())
                .eq("activity_id", activity.getId());

        Order order = orderService.getOne(wrapper);

//        已经购买过了
        if (order != null) {
            model.addAttribute("errmsg", RespBeanEnum.REPEAT_ERROR.getMessage());
            return "enlistFail";
        }

        //去redis中判断
//        SecKillOrder o = (SecKillOrder) redisTemplate.opsForValue().get("order:" + user.getId() + ":" + goodsId);
//        if (o != null) {  // 说明已经抢购过了
//            model.addAttribute("errmsg", RespBeanEnum.REPEAT_ERROR.getMessage());
//            return "secKillFail";
//        }


        //开始抢购

        Order order2 = orderService.seckill(Ticket, request, response, activity);
        if (order2 == null) {
            model.addAttribute("errmsg", RespBeanEnum.ENTRY_STOCK.getMessage());
            return "enlistFail";
        }

        //报名成功
        model.addAttribute("order", order2);
        model.addAttribute("activity", activity);
        model.addAttribute("successMsg", "报名成功！");
        return "enlistSuccess";

    }


    @Resource
    private OrderMapper orderMapper;


    @RequestMapping("/myActivity")
    public String myActivity(@CookieValue("Ticket") String Ticket, HttpServletRequest request, Model model) {
        User user = (User) redisTemplate.opsForValue().get("user:" + Ticket);

        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("user_mobile", user.getMobile());
        List<Order> orderList = orderMapper.selectList(wrapper);

        ArrayList<Activity> list = new ArrayList<>();
        for (Order order : orderList) {
            if (order.getActivityId() != null) {
                Activity activity = activityMapper.selectById(order.getActivityId());
                if (activity != null) {
                    list.add(activity);
                }
            }
        }

        model.addAttribute("orderList", orderList);
        model.addAttribute("activityList", list);

        return "myActivity";
    }




    @Resource
    private UserMapper userMapper;
    @RequestMapping("/enlistList/{id}")
    public String enlistList(@PathVariable(value = "id") String activityId,Model model) {

        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("activity_id",activityId);

        List<Order> orderList = orderMapper.selectList(queryWrapper);

        ArrayList<User> userList = new ArrayList<>();
        for (Order order : orderList) {
            Integer userId = order.getUserId();
            User user = userMapper.selectById(userId);
            userList.add(user);
        }

        model.addAttribute("orderList",orderList);
        model.addAttribute("userList",userList);


        return "enlistList";
    }


}
